<template>
    <el-dialog
            :title="!dataForm.alertId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="预警ID" prop="alertId">
                        <el-input v-model.number="dataForm.alertId" placeholder="预警ID"></el-input>
                </el-form-item>
                <el-form-item label="客户ID" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户ID"></el-input>
                </el-form-item>
                <el-form-item label="触发预警的风险指标ID" prop="indicatorId">
                        <el-input v-model.number="dataForm.indicatorId" placeholder="触发预警的风险指标ID"></el-input>
                </el-form-item>
                <el-form-item label="当前指标值" prop="currentValue">
                        <el-input v-model="dataForm.currentValue" placeholder="当前指标值"></el-input>
                </el-form-item>
                <el-form-item label="触发预警的阈值" prop="alertThreshold">
                        <el-input v-model="dataForm.alertThreshold" placeholder="触发预警的阈值"></el-input>
                </el-form-item>
                <el-form-item label="预警时间" prop="alertTime">
                        <el-date-picker
                                v-model="dataForm.alertTime"
                                type="datetime"
                                placeholder="预警时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="预警状态" prop="alertStatus">
                        <el-input v-model="dataForm.alertStatus" placeholder="预警状态"></el-input>
                </el-form-item>
                <el-form-item label="是否已通知相关人员" prop="notified">
                        <el-input v-model="dataForm.notified" placeholder="是否已通知相关人员"></el-input>
                </el-form-item>
                <el-form-item label="预警信息描述" prop="alertMessage">
                        <el-input v-model="dataForm.alertMessage" placeholder="预警信息描述"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            alertId: null,
            customerId: null,
            indicatorId: null,
            currentValue: null,
            alertThreshold: null,
            alertTime: null,
            alertStatus: null,
            notified: null,
            alertMessage: null
          },
          dataRule: {
            alertId: [
                        { required: true, message: '预警ID不能为空', trigger: 'blur' }
            ],
            customerId: [
                        { required: true, message: '客户ID不能为空', trigger: 'blur' }
            ],
            indicatorId: [
                        { required: true, message: '触发预警的风险指标ID不能为空', trigger: 'blur' }
            ],
            currentValue: [
                        { required: true, message: '当前指标值不能为空', trigger: 'blur' }
            ],
            alertThreshold: [
                        { required: true, message: '触发预警的阈值不能为空', trigger: 'blur' }
            ],
            alertTime: [
                        { required: true, message: '预警时间不能为空', trigger: 'blur' }
            ],
            alertStatus: [
                        { required: true, message: '预警状态不能为空', trigger: 'blur' }
            ],
            notified: [
                        { required: true, message: '是否已通知相关人员不能为空', trigger: 'blur' }
            ],
            alertMessage: [
                        { required: true, message: '预警信息描述不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.alertId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.alertId = id || 0
            if (this.dataForm.alertId) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_risk_alert/info/${this.dataForm.alertId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appcustomerriskalert
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_risk_alert/${!this.dataForm.alertId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>